import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import update from 'react-addons-update';
import Utils from './../../../utils'
import DB from './../../../app/db';
import StorageUtils from './../../../app/storage'
import { Control } from 'react-keeper'

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    ...PageConst,
    showWarn: false,
    warnTimer: null,
    codeTimer: null,
    warnMessage: '',
    count: 60,
    showCodeTimer: true,
    vcode: '',
    userInfo: {
      certNum: '',
      telNum: '',
      vcode: ''
    }
  },

  onGetVCode: function () {

    let t = this;

    let result = Utils.checkPhone(this.data.userInfo.telNum);

    if (!StorageUtils.getStorage().has('vcode').has) {
      if (result.success) {
        if (this.data.showCodeTimer) {
          let vcode = Utils.MathRand();
          StorageUtils.getStorage().set('vcode', vcode);
          console.log(vcode);
          DB.WechatAPI.sendVcode({
            phone: this.data.userInfo.telNum,
            vcode: vcode
          }).then(
            function (content) {
              t.data.codeTimer = setInterval(function () {
                var count = t.data.count;
                t.data.showCodeTimer = update(t.data.showCodeTimer, {$set: false});
                count -= 1;
                if (count < 1) {
                  t.data.showCodeTimer = update(t.data.showCodeTimer, {$set: true});
                  count = 60;
                  StorageUtils.getStorage().remove('vcode');
                  clearInterval(t.data.codeTimer);
                }
                t.data.count = update(t.data.count, {$set: count});

                t.updateComponent();
              }.bind(t), 1000);
            },
            function (error) {
              console.log(error);
            });
        }
      } else {
        this.data.userInfo = update(this.data.userInfo, {telNum: {$set: ''}});
        window.document.getElementById('telNum').focus();
        this.showWarnMessage(result.message);
      }
      this.updateComponent();

    } else {
      alert('请不要重复操作,过一分钟之后再尝试!')
      if (result.success) {
        if (t.data.codeTimer == null) {
          t.data.codeTimer = setInterval(function () {
            var count = t.data.count;
            t.data.showCodeTimer = update(t.data.showCodeTimer, {$set: false});
            count -= 1;
            if (count < 1) {
              t.data.showCodeTimer = update(t.data.showCodeTimer, {$set: true});
              count = 60;
              StorageUtils.getStorage().remove('vcode');
              clearInterval(t.data.codeTimer);
            }
            t.data.count = update(t.data.count, {$set: count});

            t.updateComponent();
          }.bind(t), 1000);
        }
      } else {
        this.data.userInfo = update(this.data.userInfo, {telNum: {$set: ''}});
        window.document.getElementById('telNum').focus();
        this.showWarnMessage(result.message);
      }
    }
  },


  onEnterInfo: function () {



    let resultCertNum = Utils.checkCard(this.data.userInfo.certNum);
    if (!resultCertNum.success) {
      this.data.userInfo = update(this.data.userInfo, {certNum: {$set: ''}});
      window.document.getElementById('certNum').focus();
      this.showWarnMessage(resultCertNum.message);
      return;
    }

    let resultTelNum = Utils.checkPhone(this.data.userInfo.telNum);

    if (!resultTelNum.success) {
      this.data.userInfo = update(this.data.userInfo, {telNum: {$set: ''}});
      window.document.getElementById('telNum').focus();
      this.showWarnMessage(resultTelNum.message);
      return;
    }

    if (this.data.userInfo.vcode !== StorageUtils.getStorage().get('vcode')) {
      this.data.userInfo = update(this.data.userInfo, {vcode: {$set: ''}});
      window.document.getElementById('vcode').focus();
      this.showWarnMessage('验证码错误,请重新输入!');
      return;
    }

    this.data.userInfo = update(this.data.userInfo, {$merge: {...StorageUtils.getStorage().get('user')}});

    DB.WechatAPI.binding(this.data.userInfo).then(
      function (content) {
        Control.go('/bindinghome/ok');
      },
      function (error) {
        console.log(error);
      });
  },

  showWarnMessage: function (message) {

    this.data.warnMessage = update(this.data.warnMessage, {$set: message});
    this.data.showWarn = update(this.data.showWarn, {$set: true});
    this.data.warnTimer = setTimeout(()=> {
      this.data.showWarn = update(this.data.showWarn, {$set: false});
      this.updateComponent();
    }, 3000);

    this.updateComponent();
  },

  onChangeVcode: function (vcode) {

    this.data.userInfo = update(this.data.userInfo, {vcode: {$set: vcode}});
    console.log(this.data.userInfo.vcode);

    this.updateComponent();
  },

  onChangeCertNum: function (certNum) {
    this.data.userInfo = update(this.data.userInfo, {certNum: {$set: certNum}});
    this.updateComponent();
  },

  onChangeTeleNumber: function (telNum) {
    this.data.userInfo = update(this.data.userInfo, {telNum: {$set: telNum}});
    this.updateComponent();
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }
});
